import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class CustomByteBuffer {

    private ByteBuffer byteBuffer;

    public CustomByteBuffer(Integer length) {
        byteBuffer = ByteBuffer.allocate(length);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }

    public CustomByteBuffer(byte[] data) {
        byteBuffer = ByteBuffer.wrap(data);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }

    public void order(ByteOrder order){
        byteBuffer.order(order);
    }

    public void writeByte(int i){
        this.byteBuffer.put((byte) i);
    }

    public void writeByte(byte b){
        this.byteBuffer.put(b);
    }

    public byte readByte(){
        return this.byteBuffer.get();
    }

    public void writeInt(int i){
        this.byteBuffer.putInt(i);
    }

    public int readInt(){
        return this.byteBuffer.getInt();
    }
    public int readIntByByte(){
        int i;
        i = readByte();
        i &= 0xff;
        i |= ((long) readByte() << 8);
        i &= 0xffff;
        i |= ((long) readByte() << 16);
        i &= 0xffffff;
        i |= ((long) readByte() << 24);
        return i;

    }

    public float readFloat(){
        return Float.intBitsToFloat(this.byteBuffer.getInt());
    }

 

    public void writeFloat(float f){
         this.byteBuffer.putFloat(f);
    }

    public void writeString(String s){
        char[] chars = s.toCharArray();

        int j = 0;
        for (char c : chars) {
            byte d = Integer.valueOf(c).byteValue();
            byteBuffer.put(d);
            j = j + 1;
        }
    }



    public String readString(int length){
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++) {
            sb.append(Character.getNumericValue(Integer.valueOf(byteBuffer.get())));
        }
        return sb.toString();

    }


    public void position(int p){
        byteBuffer.position(p);
    }

    public byte[] toArray(){
        return byteBuffer.array();
    }




    @Override
    public String toString() {
        return Arrays.toString(byteBuffer.array());
    }
}
